package com.clearance.mine_clearance.bot;

import com.alibaba.fastjson.JSON;
import com.clearance.mine_clearance.config.Constants;
import com.clearance.mine_clearance.entry.Config;
import com.clearance.mine_clearance.untils.*;
import org.apache.commons.lang3.StringUtils;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.AnswerCallbackQuery;
import org.telegram.telegrambots.meta.api.methods.groupadministration.CreateChatInviteLink;
import org.telegram.telegrambots.meta.api.methods.groupadministration.GetChatMember;
import org.telegram.telegrambots.meta.api.methods.send.SendDice;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.send.SendPhoto;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageCaption;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.*;
import org.telegram.telegrambots.meta.api.objects.chatmember.ChatMember;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class MineClearanceBot extends TelegramLongPollingBot {
    @Override
    public void onUpdateReceived(Update update) {
        System.out.println(JSON.toJSONString(update));
        SendMessage sendMessage = null;
        SendPhoto sendPhoto = null;
        EditMessageCaption editMessageCaption = null;
        AnswerCallbackQuery answerCallbackQuery = null;
        if (update.getCallbackQuery() != null) {
            TgUserUntils.initMessageUser(update);
            //初始化用户信息;
            CallbackQuery callbackQuery = update.getCallbackQuery();
            String text = callbackQuery.getData();
            if (text.contains(Constants.redPacket)) {
                editMessageCaption = BotUntils.robRed(callbackQuery);
            } else if (text.contains(Constants.MYAMOUNT)) {
                answerCallbackQuery = BotUntils.getMyAmount(callbackQuery);
            } else if (text.equals(Constants.promotionLink)) {
                sendMessage = InviteLinkUntils.promotionLink(update);
            } else if (text.contains(Constants.toDayStatement)) {
                answerCallbackQuery = BotUntils.toDayStatement(callbackQuery);
            } else if (text.contains(Constants.yearDayStatement)) {
                answerCallbackQuery = BotUntils.yearDayStatement(callbackQuery);
            }else if(text.contains(Constants.promotionSelect)){
                answerCallbackQuery = BotUntils.promotionSelect(callbackQuery);
            }
        } else {
            TgUserUntils.initMessageUser(update);
            Message message = update.getMessage();
            if (!StringUtils.isEmpty(message.getText())) {
                String text = message.getText();
                //说明是常规消息发送,判断是否为投注信息;
                if (text.contains(Constants.start)) {
                    sendMessage = BotUntils.startInit(update);
                }else if (text.contains("-")) {
                    String[] split = text.split("-");
                    if (NumberUntils.getStringType(split[0]) && NumberUntils.isLeiNumber(split[1])) {
                        //说明是投注信息;
                        sendPhoto = BotUntils.giveRedEnvelopes(update);
                    } else {
                        Config config = RedisUntils.getConfig();
                        sendMessage = SendMessageUntils.initSendMessage(update.getMessage().getChatId().toString(), "❌发送失败,金额必须大于【" + config.getMinRedPacket() + "】" +
                                "小于【" + config.getMinRedPacket() + "】且尾数不可小于【0】或大于【9】;");
                        sendMessage.setReplyToMessageId(update.getMessage().getMessageId());
                    }
                } else if (text.contains(Constants.balance) || text.contains(Constants.YE)) {
                    sendMessage = BotUntils.getBlance(update);
                } else if (text.equals(Constants.topUp)) {
                    //账户充值;
                    sendMessage = BotUntils.topUp(update, "充值请联系财务⬇⬇⬇⬇⬇");
                } else if (text.equals(Constants.withdraw)) {
                    //账户提现;
                    sendMessage = BotUntils.topUp(update, "提现请联系财务⬇⬇⬇⬇⬇");
                } else if (text.equals(Constants.runningWaterStatistics)) {
                    //流水统计
                    sendMessage = BotUntils.runningWaterStatistics(update);
                } else if (text.equals(Constants.customerService)) {
                    //客服;
                    sendMessage = BotUntils.kefu(update, "联系客服⬇⬇⬇⬇⬇");
                } else if (text.equals(Constants.accountInformation)) {
                    //我的信息;
                    sendMessage = BotUntils.accountInformation(update);
                }else if(text.contains(Constants.groupUrl)){
                    sendMessage = BotUntils.groupUrl(update);
                }
            }
            if (update.getMessage().getNewChatMembers() != null) {
                //说明有新成员进群组;
                List<User> newChatMembers = update.getMessage().getNewChatMembers();
                if (newChatMembers != null && !newChatMembers.isEmpty()) {
                    TgUserUntils.initMessage(update);
                }
            }
        }
        try {
            if (sendMessage != null) {
                execute(sendMessage);
            }
            if (sendPhoto != null) {
                execute(sendPhoto);
            }
            if (editMessageCaption != null) {
                execute(editMessageCaption);
            }
            if (answerCallbackQuery != null) {
                execute(answerCallbackQuery);
            }
        } catch (TelegramApiException e) {
            throw new RuntimeException(e);
        }
    }




    public void sendMessage(SendMessage sendMessage) {
        try {
            execute(sendMessage);
        } catch (TelegramApiException e) {
            throw new RuntimeException(e);
        }
    }


    public Message sendDice(SendDice sendDice) {
        Message message = null;
        try {
            message = execute(sendDice);
        } catch (TelegramApiException e) {
            throw new RuntimeException(e);
        }
        return message;
    }

    public void sendMessage(AnswerCallbackQuery answerCallbackQuery) {
        try {
            execute(answerCallbackQuery);
        } catch (TelegramApiException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getBotUsername() {
        return Constants.botName;
    }


    @Override
    public String getBotToken() {
        return Constants.token;
    }


}
